package cn.hongwei.eduservice.controller.admin;


import cn.hongwei.eduservice.entity.EduTeacher;
import cn.hongwei.eduservice.entity.TeacherQuery;
import cn.hongwei.eduservice.feign.OSSFeign;
import cn.hongwei.eduservice.service.EduTeacherService;
import cn.hongwei.result.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Great
 * @since 2021-06-22
 */
@Slf4j
@CrossOrigin //跨域
@RestController
@RequestMapping("/edu")
public class EduTeacherController {

    //注入业务层
    @Autowired
    private EduTeacherService teacherService;
    //注入访问oss远程服务的接口
    @Autowired
    private OSSFeign ossFeign;

    //做一个简单的测试,查询所有
    @GetMapping("/findAll")
    public R findAll() {
        //int i=1/0;//测试日志
        List<EduTeacher> list = teacherService.findAll();
        return R.ok().data("list", list);
    }

    //逻辑删除
    //@Transactional
    @DeleteMapping("/delete/{id}")
    public R deleteById(@PathVariable String id) {
        /*
        * 删除讲师之前先查询讲师,因为讲师里面有头像字段
        * 还要根据头像名称去远程服务删除头像
        * */
        EduTeacher teacher = teacherService.getById(id);
        //删除
        boolean b = teacherService.removeById(id);
        if(b){
            //这里由于远程调用可能会超时,所以我们采用服务降级
            //判断查询出来的数据是否有头像
            if(!StringUtils.isEmpty(teacher.getAvatar())){//有头像,删除
                ossFeign.delete(teacher.getAvatar(),"avatar");
            }
            return R.ok().message("删除成功");
        }else {
            return R.error().message("数据不存在");
        }



    }

    //6、分页查询讲师集合    pageNum  pageSize
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public R listPage(@ApiParam("页码") @PathVariable("pageNum") Integer pageNum,
                      @ApiParam("查询记录条数") @PathVariable("pageSize") Integer pageSize,
                      //@RequestBody //表示获取请求体json转为对象
                      //请求参数 使用pojo或者  requestParam方式获取
                      TeacherQuery query
    ) {
        // 业务bean
        Page<EduTeacher> page = new Page<>(pageNum, pageSize);
        page = teacherService.getPageByCondition(page, query);
        return R.ok().data("page", page);
    }

    //新增老师
    @PostMapping("/save")
    public R save(@RequestBody EduTeacher teacher) {
        System.out.println("hello"+teacher);
        boolean save = teacherService.save(teacher);
        if (save) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    //根据id更新
    @PutMapping("/update")
    public R updateById(@RequestBody EduTeacher teacher) {
        boolean b = teacherService.updateById(teacher);
        if (b) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    //批量删除
    @DeleteMapping("/batchDel")
    public R batchDel(@RequestBody List<String> ids){
        teacherService.removeByIds(ids);
        return R.ok();
    }

    //通过id查询老师
    @GetMapping("/get/{id}")
    public R getById(@PathVariable("id") String id){
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().message("根据id查询成功").data("item", teacher);
    }
}

